/*
	       File: ucDirectory.cs
	Create Date: 2007-08-16
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using GuerillaProgrammer.FileProcessor.Settings;

namespace GuerillaProgrammer.FileProcessor
{
    public partial class ucDirectory : UserControl
    {
        #region Private member fields
        private Configuration _Configuration;
        private ConfigurationDirectory _Directory;
        #endregion

        #region Constructors and cleanup methods
        public ucDirectory()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        private void LoadFileTypes()
        {
            lstFileType.Items.Clear();
            if (null != _Directory.DirectoryFileTypes)
            {
                foreach (string fileTypeID in _Directory.DirectoryFileTypes)
                {
                    ConfigurationFileType configFileType = ConfigurationManager.GetFileTypeByID(_Configuration, fileTypeID);
                    lstFileType.Items.Add(string.Format("{0} - {1}", configFileType.ID, configFileType.Name));
                }
            }
        }
        private void OnFinishedUpdate()
        {
            if (null != FinishedUpdate)
            {
                FinishedUpdate(_Directory.Name, _Directory.ID);
            }
        }
        #endregion

        #region Events handlers
        private void btnSelectFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (DialogResult.OK == fbd.ShowDialog())
            {
                txtPath.Text = fbd.SelectedPath;
            }
        }
        private void btnProperties_Click(object sender, EventArgs e)
        {
            frmProperties propertiesForm = new frmProperties();
            propertiesForm.LoadDirectoryProperties(_Directory.Parameters);
            propertiesForm.UpdateProperties +=new frmProperties.UpdatePropertiesDelegate(this.UpdateDirectoryProperties);
            propertiesForm.ShowDialog();
        }
        private void btnFileTypeAdd_Click(object sender, EventArgs e)
        {
            frmSelectFileType fileTypeFrm = new frmSelectFileType();
            fileTypeFrm.LoadFileTypes(_Configuration);
            if (DialogResult.OK == fileTypeFrm.ShowDialog())
            {
                ConfigurationFileType fileType = fileTypeFrm.FileType;
                if (null == _Directory.DirectoryFileTypes)
                {
                    _Directory.DirectoryFileTypes = new List<string>();
                }
                _Directory.DirectoryFileTypes.Add(fileType.ID);
                LoadFileTypes();                
            }
        }
        private void btnFileTypeDelete_Click(object sender, EventArgs e)
        {
            if (-1 == lstFileType.SelectedIndex)
            {
                return;
            }

            object listItem = lstFileType.Items[lstFileType.SelectedIndex];
            lstFileType.Items.Remove(listItem);
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            _Directory.Name = txtName.Text;
            _Directory.Description = txtDescription.Text;
            _Directory.Path = txtPath.Text;

            if (null == _Directory.DirectoryFileTypes)
            {
                _Directory.DirectoryFileTypes = new List<string>();
            }
            _Directory.DirectoryFileTypes.Clear();
            foreach (string fileType in lstFileType.Items)
            {
                string[] pieces = fileType.Split(new char[] { '-' });
                _Directory.DirectoryFileTypes.Add( pieces[0].Trim() );
            }
            OnFinishedUpdate();
        }
        #endregion

        #region Public interface
        public void LoadDirectory(Configuration configuration, ConfigurationDirectory directory)
        {
            if (null != configuration && null != directory)
            {
                _Configuration = configuration;
                _Directory = directory;
                txtName.Text = _Directory.Name;
                txtPath.Text = _Directory.Path;
                txtDescription.Text = _Directory.Description;
                LoadFileTypes();
            }
        }
        public void UpdateDirectoryProperties(List<Property> properties)
        {
            _Directory.Parameters = new List<ConfigurationDirectoryParameter>();
            foreach (Property property in properties)
            {
                ConfigurationDirectoryParameter parameter = new ConfigurationDirectoryParameter();
                parameter.Name = property.Name;
                parameter.Value = property.Value;
                _Directory.Parameters.Add(parameter);
            }
        }
        public delegate void FinishedUpdateDelegate(string name, string ID);
        public event FinishedUpdateDelegate FinishedUpdate;
        #endregion
    }
}
